package com.springcloud.alibabanacosdiscoveryclientcommon;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class AlibabaNacosDiscoveryClientCommonApplication {
    public static void main(String[] args) {
        SpringApplication.run(AlibabaNacosDiscoveryClientCommonApplication.class, args);
    }

    @Slf4j
    @RestController
    static class TestController {
        @Autowired
        LoadBalancerClient loadBalancerClient;
        @Autowired
        RestTemplate restTemplate;
        @Autowired
        private WebClient.Builder webClientBuilder;
        @Autowired
        Client client;

        @GetMapping("test")
        public String test() {
            ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
            String url = serviceInstance.getUri() + "?name=" + "juzhi";
            RestTemplate restTemplate = new RestTemplate();
            String result = restTemplate.getForObject(url, String.class);
            return "Invoke:" + url + ",return:" + result;
        }

        @GetMapping("testRestTemplateBatter")
        public String testRestTemplateBatter() {
            String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server?name=orange", String.class);
            return result;
        }

        @GetMapping("testWebClient")
        public Mono<String> testWebClient() {
            Mono<String> result = webClientBuilder.build()
                    .get()
                    .uri("http://alibaba-nacos-discovery-server/?name=orangeWebClent")
                    .retrieve()
                    .bodyToMono(String.class);
            return result;
        }


        @GetMapping("testFeign")
        public String testFeign() {
            String result = client.hello("orangeFeign");
            return "Return:" + result;
        }


        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }

        @Bean
        @LoadBalanced
        public WebClient.Builder loadBalancedWebClientBuilder() {
            return WebClient.builder();
        }

    }

    @FeignClient("alibaba-nacos-discovery-server")
    interface Client {
        @GetMapping("/")
        String hello(@RequestParam(name = "name") String name);
    }

}
